package pck_tramitedocu.pck_web;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import pck_tramitedocu.pck_src.pck_facade.itf_facade;
import pck_tramitedocu.pck_src.pck_pojos.TramiteDocuException;
import pck_tramitedocu.pck_src.pck_utiles.Cls_Parametros;
import pck_tramitedocu.pck_src.pck_utiles.cls_utilitarios;

public class svl_gestion_facade_web extends HttpServlet {

    public static itf_facade facadeInterface;

    public void init(ServletConfig config)
            throws ServletException {
        super.init(config);
         String propsFilePath = "/WEB-INF/app.properties";
        try {
            Properties properties = new Properties();
            InputStream inputStream = config.getServletContext().getResourceAsStream(propsFilePath);
            properties.load(inputStream);
            inputStream.close();
            getServletContext().setAttribute("properties", properties);
        } catch (Exception e) {
            Logger.getLogger(svl_gestion_facade_web.class.getName()).log(Level.SEVERE, null, e);
            throw new ServletException("Error loading properties file from path " + propsFilePath, e);
        }
        requestContext();
        loadParametrosGlobales();
    }

    private void loadParametrosGlobales() {
        if(cls_utilitarios.isLinux()){
            pck_tramitedocu.pck_src.pck_utiles.Cls_Parametros.RUTA_DOCUMENTOS_SERVIDOR2 = ((Properties) getServletContext().getAttribute("properties")).getProperty(Cls_Parametros.URL_PROPERTIES_LINUX);
        }else{
            pck_tramitedocu.pck_src.pck_utiles.Cls_Parametros.RUTA_DOCUMENTOS_SERVIDOR2 = ((Properties) getServletContext().getAttribute("properties")).getProperty(Cls_Parametros.URL_PROPERTIES_WINDOWS);
        }        
        try {
            cls_utilitarios.setArrayDocumentos(svl_gestion_facade_web.facadeInterface.obtenerTipoDocumentoFacade(null));
        } catch (TramiteDocuException ex) {
            cls_utilitarios.setArrayDocumentos(null);
        }
    }

    private void requestContext() {
        System.out.println("Cargando Context De Tramite Documentario");
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        facadeInterface = (itf_facade) context.getBean("facadeBean");
        System.out.println("Context Cargado Tramite Documentario");
    }

    public static itf_facade getFacade() {
        return facadeInterface;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    public String getServletInfo() {
        return "Short description";
    }
}